#!/bin/bash

# 脚本名称: update_github_hosts.sh
# 描述: 将github-hosts/hosts文件中的内容添加到系统hosts文件中，以加速GitHub访问
# 作者: Craft AI Assistant
# 创建日期: $(date +%Y-%m-%d)

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

# 检查是否以root权限运行
if [ "$(id -u)" -ne 0 ]; then
    echo -e "${RED}错误: 此脚本需要root权限来修改hosts文件${NC}"
    echo -e "请使用 ${YELLOW}sudo $0${NC} 重新运行此脚本"
    exit 1
fi

# 定义文件路径
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GITHUB_HOSTS_FILE="${SCRIPT_DIR}/hosts"
SYSTEM_HOSTS_FILE="/etc/hosts"
BACKUP_HOSTS_FILE="/etc/hosts.bak.$(date +%Y%m%d%H%M%S)"

# 检查github-hosts文件是否存在
if [ ! -f "$GITHUB_HOSTS_FILE" ]; then
    echo -e "${RED}错误: github-hosts文件不存在: ${GITHUB_HOSTS_FILE}${NC}"
    exit 1
fi

# 备份当前hosts文件
echo -e "${YELLOW}备份当前hosts文件到 ${BACKUP_HOSTS_FILE}...${NC}"
cp "$SYSTEM_HOSTS_FILE" "$BACKUP_HOSTS_FILE"

# 提取github-hosts文件中的内容
echo -e "${YELLOW}提取github-hosts文件中的内容...${NC}"
GITHUB_HOSTS_CONTENT=$(sed -n '/# Generated by hosts-generator start/,/# Generated by hosts-generator end/p' "$GITHUB_HOSTS_FILE")

# 检查系统hosts文件中是否已包含github-hosts标记
if grep -q "# Generated by hosts-generator start" "$SYSTEM_HOSTS_FILE"; then
    echo -e "${YELLOW}系统hosts文件中已包含github-hosts内容，正在更新...${NC}"
    # 删除旧的github-hosts内容
    sed -i '/# Generated by hosts-generator start/,/# Generated by hosts-generator end/d' "$SYSTEM_HOSTS_FILE"
fi

# 添加github-hosts内容到系统hosts文件
echo -e "${YELLOW}添加github-hosts内容到系统hosts文件...${NC}"
echo "" >> "$SYSTEM_HOSTS_FILE"
echo "$GITHUB_HOSTS_CONTENT" >> "$SYSTEM_HOSTS_FILE"

echo -e "${GREEN}完成! GitHub hosts已成功更新!${NC}"
echo -e "原hosts文件已备份到: ${YELLOW}${BACKUP_HOSTS_FILE}${NC}"

# 刷新DNS缓存（根据不同系统可能需要不同的命令）
echo -e "${YELLOW}尝试刷新DNS缓存...${NC}"

# 尝试使用不同的方法刷新DNS缓存
DNS_FLUSHED=false

# 对于使用 systemd-resolved 的系统
if command -v resolvectl &> /dev/null; then
    resolvectl flush-caches
    echo -e "${GREEN}DNS缓存已刷新 (resolvectl)${NC}"
    DNS_FLUSHED=true
elif command -v systemd-resolve &> /dev/null; then
    systemd-resolve --flush-caches
    echo -e "${GREEN}DNS缓存已刷新 (systemd-resolve)${NC}"
    DNS_FLUSHED=true
fi

# 对于使用 nscd 的系统
if command -v systemctl &> /dev/null && systemctl is-active --quiet nscd; then
    systemctl restart nscd
    echo -e "${GREEN}DNS缓存已刷新 (nscd)${NC}"
    DNS_FLUSHED=true
fi

# 对于较旧的Ubuntu/Debian系统
if [ -f /etc/init.d/dns-clean ]; then
    /etc/init.d/dns-clean start
    echo -e "${GREEN}DNS缓存已刷新 (dns-clean)${NC}"
    DNS_FLUSHED=true
fi

# 对于macOS系统
if command -v dscacheutil &> /dev/null; then
    dscacheutil -flushcache
    if command -v killall &> /dev/null; then
        killall -HUP mDNSResponder 2>/dev/null || true
    fi
    echo -e "${GREEN}DNS缓存已刷新 (macOS)${NC}"
    DNS_FLUSHED=true
fi

# 如果所有方法都失败了
if [ "$DNS_FLUSHED" = false ]; then
    echo -e "${YELLOW}提示: 未能自动刷新DNS缓存${NC}"
    echo -e "${YELLOW}你可以尝试以下方法之一:${NC}"
    echo -e "1. 重启网络服务: ${YELLOW}sudo service network-manager restart${NC}"
    echo -e "2. 重启网络接口: ${YELLOW}sudo ifdown -a && sudo ifup -a${NC}"
    echo -e "3. 重启系统"
fi

echo -e "${GREEN}现在你应该可以更快地访问GitHub了!${NC}"